package com.wm.eduService.controller;


import com.wm.commonutils.R;
import com.wm.commonutils.result.ResultCode;
import com.wm.eduService.entity.EduChapter;
import com.wm.eduService.entity.vo.ChapterVo;
import com.wm.eduService.service.EduChapterService;
import com.wm.servicebase.exceptionhandle.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author sshdg
 * @since 2020-10-01
 */
@RestController
//@CrossOrigin
@Api(tags = "课程章节管理")
@RequestMapping("/eduService/chapter")
public class EduChapterController {

    @Autowired
    EduChapterService chapterService;


    @ApiOperation("根据id查询课程大纲列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("list", chapterVoList);
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        boolean save = chapterService.save(chapter);
        if (!save){
            throw GuliException.from(ResultCode.SAVE_ERROR);
        }
        return R.ok();
    }

    @ApiOperation("修改章节")
    @PutMapping("/updateChapter")
    public R updateChapter(@RequestBody  EduChapter chapter) {
        boolean updateResult = chapterService.updateById(chapter);
        if (!updateResult) {
            throw GuliException.from(ResultCode.UPDATE_ERROR);
        }
        return R.ok();

    }

    @ApiOperation("根据id删除章节")
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        chapterService.deleteChapter(chapterId);
        return R.ok();
    }
}

